package com.wuyou.controller;


import cn.hutool.core.collection.CollUtil;
import com.wuyou.domain.Result;
import com.wuyou.domain.bo.MongoHouseBO;
import com.wuyou.domain.po.MongoHouse;
import com.wuyou.domain.vo.MongoHouseVO;
import com.wuyou.em.ResultEnum;
import com.wuyou.service.MongoHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("mapLook")
@Api(value = "地图找房接口")
public class MapLookHouse {

    @Autowired
    private MongoHouseService mongoHouseService;


    @GetMapping("queryHouse")
    @ApiOperation(value = "根据经纬度以及缩放比例查询附近1.5公里房源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lng", value = "经度", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "纬度", required = true, paramType = "query"),
            @ApiImplicitParam(name = "zoom", value = "缩放比", required = true, paramType = "query")
    })
    public Result mapLookHouse(@RequestParam("lng") Double lng,
                               @RequestParam("lat")Double lat,
                               @RequestParam("zoom") Integer zoom){
        try {
            List<MongoHouseBO> list = mongoHouseService.mapLookHouse(lng,lat,zoom);
            if (CollUtil.isEmpty(list)){
                return Result.R(ResultEnum.NO_LISTINGS_YET);
            }
            MongoHouseVO vo = new MongoHouseVO();
            List<MongoHouseVO> vos = new ArrayList<>();
            for (MongoHouseBO bo : list) {
                BeanUtils.copyProperties(bo,vo);
                vos.add(vo);
            }
            return Result.OK().setData(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.R(ResultEnum.NO_LISTINGS_YET);
        }
    }

}
